//
// Created by hp on 2020-01-01.
//

#include "Game.h"
GLuint Game::Width = 800;
GLuint Game::Height = 600;

void Game::convVert(float lx,float ly,float width,float height, float vertices[]) {
    //先把坐标转换到（-1,1）的范围内
    float fx = lx / Width * 2 - 1;
    float fy = ly / Height * 2 - 1;
    float fw = width / Width * 2;
    float fh = height / Height * 2;

    int row = 0;
    //top right
    vertices[0 + row * 8] = fx + fw;
    vertices[1 + row * 8] = fy + fh;
    vertices[2 + row * 8] = 0.0f;

    vertices[3 + row * 8] = 0.0f;
    vertices[4 + row * 8] = 0.0f;
    vertices[5 + row * 8] = 0.0f;

    vertices[6 + row * 8] = 1.0f;
    vertices[7 + row * 8] = 1.0f;

    //bottom right
    row++;
    vertices[0 + row * 8] = fx + fw;
    vertices[1 + row * 8] = fy;
    vertices[2 + row * 8] = 0.0f;

    vertices[3 + row * 8] = 0.0f;
    vertices[4 + row * 8] = 0.0f;
    vertices[5 + row * 8] = 0.0f;

    vertices[6 + row * 8] = 1.0f;
    vertices[7 + row * 8] = 0.0f;

    //bottom left
    row++;
    vertices[0 + row * 8] = fx;
    vertices[1 + row * 8] = fy;
    vertices[2 + row * 8] = 0.0f;

    vertices[3 + row * 8] = 0.0f;
    vertices[4 + row * 8] = 0.0f;
    vertices[5 + row * 8] = 0.0f;

    vertices[6 + row * 8] = 0.0f;
    vertices[7 + row * 8] = 0.0f;

    //top left
    row++;
    vertices[0 + row * 8] = fx;
    vertices[1 + row * 8] = fy + fh;
    vertices[2 + row * 8] = 0.0f;

    vertices[3 + row * 8] = 0.0f;
    vertices[4 + row * 8] = 0.0f;
    vertices[5 + row * 8] = 0.0f;

    vertices[6 + row * 8] = 0.0f;
    vertices[7 + row * 8] = 1.0f;

}

Game::Game(GLuint width,GLuint height)
{
    Game::Width=width;
    Game::Height=height;
}

Game::~Game()
{

}

void Game::init() {

}

GLFWwindow* Game::createWindow()
{
    void framebuffer_size_callback(GLFWwindow* window, int width, int height);
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
    GLFWwindow* window = glfwCreateWindow(Width, Height, "Thunder", nullptr, nullptr);
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return nullptr;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return nullptr;
    }
    return window;
}

void Game::Update(GLfloat dt)
{

}
void Game::createMenu() {

}

char Game::ProcessInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
        return 'E';
    }
    else if(glfwGetKey(window,GLFW_KEY_A)==GLFW_PRESS) {
        return 'A';
    }
    else if(glfwGetKey(window,GLFW_KEY_D)==GLFW_PRESS) {

        return 'D';
    }
    else if(glfwGetKey(window,GLFW_KEY_W)==GLFW_PRESS) {

        return 'W';
    }
    else if(glfwGetKey(window,GLFW_KEY_S)==GLFW_PRESS) {

        return 'S';
    }
    else if(glfwGetKey(window,GLFW_KEY_F)==GLFW_PRESS){
        return 'F';
    }
    else if(glfwGetKey(window,GLFW_KEY_SPACE)==GLFW_PRESS){
        return ' ';
    } else
        return '0';
}

void Game::Render()
{

}